import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_common/util/logger.dart';
import 'package:flutter_common/util/toast_util.dart';
import '../../flutter_common.dart';

///输入金融的格式
///
///[decimal] 小数点位数
///
///[maxValue] 允许输入最大值
TextInputFormatter formatterMoneyInput({int decimal = 2, int? maxValue}) {
  return TextInputFormatter.withFunction((oldValue, newValue) {
    //允许字符串空
    if (newValue.text.isEmpty) return newValue;
    String  reg = r'^(([1-9]{1}\d*)|(0{1}))(\.\d{0,' + '$decimal' + r'})?$';
    bool isMoney = RegExp(reg).hasMatch(newValue.text);
    if (!isMoney) {
      return oldValue;
    }
    if (maxValue != null && (double.tryParse(newValue.text) ?? 0) > maxValue) {
      return oldValue;
    }
    return newValue;
  });
}

class FxTextFormField extends TextFormField {
  FxTextFormField(
      {Key? key,
        String? hintText = "请输入",
        TextStyle? hintStyle,
        Color enabledBorderColor = const Color(0xFFDEDEDE),
        Color disabledBorderColor = const Color(0xFFDEDEDE),
        Color? focusedBorderColor,
        EdgeInsetsGeometry? contentPadding = const EdgeInsets.fromLTRB(15, 10, 15, 10),
        super.style = const TextStyle(color: Color(0xFF333333), fontSize: 14),
        super.onChanged,
        super.onFieldSubmitted,
        super.focusNode,
        super.onEditingComplete,
        super.controller,
        super.obscureText,
        super.maxLength,
        super.autofocus,
        super.validator,
        super.onSaved,
        super.maxLines,
        super.keyboardType,
        super.enabled,
        super.minLines,
        super.textInputAction = TextInputAction.done,
        List<TextInputFormatter>? inputFormatters,
        Widget? suffixIcon,
        String? counterText = '',
        BorderRadius borderRadius = const BorderRadius.all(Radius.circular(8)),
        bool? filled,
        Color? fillColor,
      })
      : super(
      key: key,
      contextMenuBuilder: (BuildContext context, EditableTextState editableTextState) {
        return AdaptiveTextSelectionToolbar.editable(clipboardStatus: editableTextState.clipboardStatus!.value,
            onCopy: editableTextState.copyEnabled ? (){
              editableTextState.copySelection(SelectionChangedCause.toolbar);
            } : null,
            onCut: editableTextState.cutEnabled ? (){
              editableTextState.cutSelection(SelectionChangedCause.toolbar);
            } : null,
            onPaste: editableTextState.pasteEnabled ? (){
                editableTextState.pasteText(SelectionChangedCause.toolbar).catchError((e){
                  logger.error(e);
                  ToastUtil.showToast('没有权限，操作失败');
                });
            } : null,
            onSelectAll: editableTextState.selectAllEnabled ? (){
              editableTextState.selectAll(SelectionChangedCause.toolbar);
            } : null,
            anchors: editableTextState.contextMenuAnchors);
      },
      inputFormatters: (keyboardType == TextInputType.phone || keyboardType == TextInputType.number) ? [FilteringTextInputFormatter.allow(RegExp(r'[0123456789+-]'))] : inputFormatters,
      decoration: InputDecoration(
          hintText: hintText,
          hintStyle: hintStyle,
          counterText: counterText,
          contentPadding: contentPadding,
          errorMaxLines: 2,
          suffixIcon: suffixIcon,
          filled: filled,
          fillColor: fillColor,
          errorBorder: OutlineInputBorder(
              borderRadius: borderRadius,
              borderSide: BorderSide(color: enabledBorderColor)),
          focusedErrorBorder: OutlineInputBorder(
            borderRadius: borderRadius,
            borderSide: BorderSide(color: focusedBorderColor??FxTheme().backgroundColor),
          ),
          enabledBorder: OutlineInputBorder(
              borderRadius: borderRadius,
              borderSide: BorderSide(color: enabledBorderColor)),
          focusedBorder: OutlineInputBorder(
            borderRadius: borderRadius,
            borderSide: BorderSide(color: focusedBorderColor??FxTheme().backgroundColor),
          ),
          disabledBorder: OutlineInputBorder(
            borderRadius: borderRadius,
            borderSide: BorderSide(color: disabledBorderColor),
          ),
      ),
  );
}

///没有边框的输入框
class SimpleTextFiled extends TextField {
  SimpleTextFiled({
    Key? key,
    String? hintText = "请输入",
    EdgeInsetsGeometry? contentPadding =
        const EdgeInsets.fromLTRB(15, 10, 15, 10),
    super.style = const TextStyle(color: Color(0xFF333333), fontSize: 14),
    super.onChanged,
    super.focusNode,
    super.onEditingComplete,
    super.controller,
    super.obscureText,
    super.maxLength,
    super.maxLines,
    super.minLines,
    super.enabled,
    super.inputFormatters,
    super.textAlign,
    super.keyboardType,
    super.textInputAction = TextInputAction.done,
    Widget? suffixIcon,
    String? counterText = '',
    TextStyle? hintStyle = const TextStyle(color: Color(0xFFBBBBBB), fontSize: 14),
    TextStyle? counterStyle = const TextStyle(color: Color(0xFFBBBBBB), fontSize: 14),
  }) : super(
            key: key,
            decoration: InputDecoration(
              hintText: hintText,
              hintStyle: hintStyle,
              counterText: counterText,
              counterStyle: counterStyle,
              contentPadding: contentPadding,
              errorMaxLines: 2,
              suffixIcon: suffixIcon,
              errorBorder: InputBorder.none,
              focusedErrorBorder: InputBorder.none,
              enabledBorder: InputBorder.none,
              disabledBorder: InputBorder.none,
              focusedBorder: InputBorder.none,
            ));
}
